﻿using System;
using System.Collections.Generic;
using System.Linq;
using Subsembly.SmartCard;

namespace Ipmce.RemoteVoting.Common.Tools.CardReader.Utils
{
    internal class CardTerminalFactory : ICardTerminalFactory
    {
        public IList<string> GetActiveTerminalNames()
        {
            var register = new CardTerminalRegistry(true);
            register.AutoRegisterPcSc();

            return (register.Cast<CardTerminalConfig>().Where(
                terminalConfig => IsCardReaderActive(terminalConfig.AssignedName)).Select(
                    terminalConfig => terminalConfig.AssignedName)).ToList();
        }

        private static bool IsCardReaderActive(string name)
        {
            var terminal = new CardPcScTerminal(name);
            return terminal.IsCardPresent(0);
        }
    }
}